package demo.practice.array;

import java.util.*;

class KV {
    Integer k;
    Integer v;

    public KV(Integer k, Integer v) {
        this.k = k;
        this.v = v;
    }

    public Integer getK() {
        return k;
    }

    public Integer getV() {
        return v;
    }

    @Override
    public String toString() {
        return "KV{" +
                "k=" + k +
                ", v=" + v +
                '}';
    }
}

public class P373 {

    public static void main(String[] args) {
        P373 p373= new P373();
        p373.kSmallestPairs(new int[]{1,7,11},new int[]{2,4,6},7);
    }


    public List<List<Integer>> kSmallestPairs(int[] nums1, int[] nums2, int k) {

        PriorityQueue<KV> queue = new PriorityQueue<KV>((o1, o2) -> {

            Integer k1 = o1.getK();
            Integer v1 = o1.getV();

            Integer k2 = o2.getK();
            Integer v2 = o2.getV();

            if (k1 + v1 > k2 + v2) {
                return -1;
            } else if (k1 + v1 < k2 + v2) {
                return 1;
            } else {
                return k1 - k2;
            }

        });

        int i = 0;
        int j = 0;
        //List<List<Integer>> list = new ArrayList<>();


        for (int i1 : nums1) {
            for (int i2 : nums2) {
                queue.add(new KV(i1,i2) );
                if(queue.size()>k)
                    queue.poll();
            }
        }

        List<List<Integer>> list = new ArrayList<>();

        while (queue.size()>0){
            KV poll = queue.poll();
            list.add(Arrays.asList(poll.getK(),poll.getV()));
        }

        Collections.reverse(list);
        return list;
    }
}
